package com.ticker.national;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import com.ticker.national.util.Log;
import com.ticker.national.util.RedirectException;

public class SocialParseTask extends BaseParseTask {
	
	private static final String TAG = "SocialParseTask";
	
	protected int mErrorCount;
	protected int mStatusCode;
	
	private HashMap<String, String> mSocialIdMap;
	
	public SocialParseTask(HashMap<String, String> socialId, String url) {
		mSocialIdMap = socialId;
		mUrl = url;
	}

	@Override
	void doParsing(String url) {
		Log.d(TAG, "doParsing(), url : " + url);
		
		final HttpGet getRequest = new HttpGet(url);
		HttpResponse response = null;
		Source source = null;
		
		try {
			if(mClient == null)
				mClient = getHttpClient();
			response = mClient.execute(getRequest);
			final int statusCode = response.getStatusLine().getStatusCode();
			if(statusCode != HttpStatus.SC_OK) {
				Log.e("statusCode : " + statusCode);
			}

			mStatusCode = statusCode;

			if(statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
				url = response.getLastHeader("Location").getValue();
				throw new RedirectException();
			}

			source = new Source(new InputStreamReader(response.getEntity().getContent(), "EUC-KR"));

			source.getAllElements();
			
			List<Element> targetElement = source.getAllElements(HTMLElementName.TBODY);
			for(Element element: targetElement) {
				List<Element> TrElements = element.getAllElements(HTMLElementName.TR);
				for(Element trElement: TrElements) {
					List<Element> AnchorElements = trElement.getAllElements(HTMLElementName.A);
					if(AnchorElements.size() > 2) {
						String twitterId = AnchorElements.get(2).getAttributeValue("href");
						if(twitterId.contains("twitter")) {
							twitterId = twitterId.replace("http://twitter.com/", "");
							String name = AnchorElements.get(0).getTextExtractor().toString();
							mSocialIdMap.put(twitterId, name);
						}
					}
				}
			}
			
		} catch (IOException e) {
			Log.e("doParsing() IOException: " + e.getMessage());
			e.printStackTrace();
			mErrorCount++;
			if(mErrorCount < 5) {
				doParsing(url);
				Log.e("doParsing() IOException, retry");
			}
		} catch (RedirectException e) {
			Log.e("doParsing() RedirectException: " + e.getMessage());
			e.printStackTrace();
		} finally {
			source = null;
			if(mClient != null)
				mClient.close();
			mClient = null;
			mErrorCount = 0;
			
			if(mStatusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
				Log.e(TAG, "doParsing() Again");
				doParsing(url);
			}
		}
	}

	@Override
	public void onResume() {
		Log.d(TAG, "onResume()");
	}
	
	public static class SocialUrlList {
		private final String TARGET_URL = "http://www.assembly.go.kr/renew10/mem/pr/snst_list.jsp?M_idx=2_91&&sitem=&sword=&party=&display=300&start=";	// 1~295
	
		private ArrayList<String> mUrlList;
		
		public SocialUrlList() {
			mUrlList = new ArrayList<String>();
			setUrlList();
		}
		
		private void setUrlList() {
			Log.d(TAG, "setUrlList()");
			ArrayList<String> urlList = new ArrayList<String>();
			for(int i = 1; i < 2; i++) {
				urlList.add(TARGET_URL + i);
			}

			mUrlList = urlList;
		}
		
		public final ArrayList<String> getUrlList() {
			Log.d(TAG, "getUrlList()");
			if(mUrlList == null) {
				setUrlList();
			}
			
			return mUrlList;
		}
	}

}
